package com.xgpro.common.utils;


import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.xgpro.common.dingtalk.auth.AuthHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class DDMsgUtil {

    @Value("${DD_ENV.APP_KEY}")
    private String APP_KEY;

    @Value("${DD_ENV.APP_SECRET}")
    private String APP_SECRET;

    @Value("${DD_ENV.AGENTID}")
    private String AGENTID;

    public void sendMsg(String userList,String sendMsg) throws Exception {

        //获取toden
        String accessToken = AuthHelper.getAccessToken(APP_KEY, APP_SECRET);
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(userList);
        long l = Long.parseLong(AGENTID);
        request.setAgentId(l);
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent(sendMsg);
        request.setMsg(msg);

        OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,accessToken);
    }
}
